package com.meiqiongproperty.yiguanjia.fragment.home.adapter.shoppingadapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.MoreProductItemLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.shopping.ShopListBean;
import com.meiqiongproperty.yiguanjia.ui.PullToRefresh.recyclerview.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/29.
 */

public class MoreProductListAdapter extends BaseRecyclerAdapter<MoreProductListAdapter.BindingHolder> {
    Context context;
    MoreProductItemLayoutBinding binding;
    List<ShopListBean.ShoplistBean> shop_list=new ArrayList<>();
    LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;


    public MoreProductListAdapter(Context context,List<ShopListBean.ShoplistBean> shop_list) {
        this.context = context;
        this.shop_list=shop_list;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup parent) {
        binding= DataBindingUtil.inflate(inflater, R.layout.more_product_item_layout,parent,false);
        BindingHolder holder = new BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public BindingHolder getViewHolder(View view) {
        BindingHolder holder=new BindingHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final BindingHolder holder, int position, boolean isItem) {
        ShopListBean.ShoplistBean bean=shop_list.get(position);
        Glide.with(context).load(bean.getImagePath()).into(holder.binding.imgDetail);
       // Picasso.with(context).load(product.getImg_resourceId()).centerInside().into(binding.imgDetail);
        holder.binding.descriptionProduct.setText(bean.getProductName());
        holder.binding.productPrice.setText("￥"+bean.getOprice());

        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }
    }

    @Override
    public int getAdapterItemCount() {
        return shop_list.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder {
        MoreProductItemLayoutBinding binding;

        public MoreProductItemLayoutBinding getBinding() {
            return binding;
        }

        public void setBinding(MoreProductItemLayoutBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
